package com.xjscrm.console.web.chat;

import com.xjscrm.common.entity.MerchatChild;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.dto.ChatSettingsDTO;
import com.xjscrm.console.dto.ChatWwDeviceOptDTO;
import com.xjscrm.console.dto.ChatWwDeviceQueryDTO;
import com.xjscrm.console.dto.chat.setting.ChatBaseSettingOptDTO;
import com.xjscrm.console.service.customer.chat.ChatSettingsService;
import com.xjscrm.console.service.customer.childUser.ChildMerchatService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import com.xjscrm.console.vo.LoginVO;
import com.xjscrm.console.vo.WwDeviceVo;
import com.xjscrm.console.vo.chat.ChatSettingsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/15 11:54
 */
@Api(value = "通用设置", tags = "聊天设置|通用设置")
@RestController
@RequestMapping("/chat")
public class ChatBaseSettingConfController {

    @Autowired
    private ChildMerchatService childMerchatService;

    @Autowired
    private WwDeviceService wwDeviceService;

    @Autowired
    private ChatSettingsService chatSettingsService;

    @PostMapping("/base_setting/openChatNotice")
    @ApiOperation(value = "开启聊天提示音", notes = "开启聊天提示音", response = Boolean.class)
    public Object openChatNotice(@RequestBody ChatBaseSettingOptDTO opt) {
        LoginVO login = LoginContext.getLogin();
        return childMerchatService.editChatNotice(login, opt);
    }

    @PostMapping("/base_setting/chatNotice")
    @ApiOperation(value = "聊天提示音获取", notes = "聊天提示音获取", response = ChatBaseSettingOptDTO.class)
    public Object chatNotice() {
        LoginVO login = LoginContext.getLogin();
        ChatBaseSettingOptDTO result = new ChatBaseSettingOptDTO();
        MerchatChild byId = childMerchatService.getById(login.getMerId(), login.getId());

        if (Objects.isNull(byId)) {
            result.setOpenNotice(0);
            result.setOpenGroupNotice(0);
            return result;
        }
        result.setOpenNotice(byId.getChatOpenNotice());
        result.setOpenGroupNotice(ObjectUtils.defaultIfNull(byId.getChatOpenGroupNotice(), 0));
        return result;
    }


    @PostMapping("/chat_show/device/search")
    @ApiOperation(value = "聊天显示机器人列表", notes = "聊天显示机器人列表", response = WwDeviceVo.class)
    public Object deviceSearch(@RequestBody ChatWwDeviceQueryDTO query) {
        LoginVO login = LoginContext.getLogin();
        return wwDeviceService.pageWithChat(login, query);
    }

    @PostMapping("/chat_show/device/edit")
    @ApiOperation(value = "聊天显示机器人修改", notes = "聊天显示机器人修改", response = Boolean.class)
    public Object deviceSearch(@RequestBody ChatWwDeviceOptDTO chatWwDeviceOpt) {
        LoginVO login = LoginContext.getLogin();

//        if (!Objects.equals(login.getId(), login.getMerId())) {
//            throw new BusinessException("非主账号, 不允许操作");
//        }

        WwDevice wwDevice = new WwDevice();
        wwDevice.setId(chatWwDeviceOpt.getDeviceUniqueId());
        wwDevice.setMerId(login.getMerId());
        wwDevice.setOpenChat(chatWwDeviceOpt.getOpenChat());
        wwDevice.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

        return wwDeviceService.update(wwDevice);
    }

    @PostMapping("/keyword/shield/open")
    @ApiOperation(value = "开启/关闭关键词", notes = "聊天显示机器人修改", response = Boolean.class)
    public Object openKeywordShield(@RequestBody ChatSettingsDTO chatWwDeviceOpt) {
        LoginVO login = LoginContext.getLogin();

//        if (!Objects.equals(login.getId(), login.getMerId())) {
//            throw new BusinessException("非主账号, 不允许操作");
//        }
        if (Objects.isNull(chatWwDeviceOpt) || Objects.isNull(chatWwDeviceOpt.getOpenKeywordShield())) {
            throw new BusinessException("参数错误");
        }
        return chatSettingsService.openOrCloseKeywordShield(login.getMerId(), chatWwDeviceOpt.getOpenKeywordShield());
    }

    @PostMapping("/keyword/shield/save")
    @ApiOperation(value = "保存关键词消息屏蔽", notes = "保存关键词消息屏蔽", response = Boolean.class)
    public Object saveKeywordShield(@RequestBody ChatSettingsDTO chatWwDeviceOpt) {
        LoginVO login = LoginContext.getLogin();

//        if (!Objects.equals(login.getId(), login.getMerId())) {
//            throw new BusinessException("非主账号, 不允许操作");
//        }

        if (Objects.isNull(chatWwDeviceOpt)) {
            throw new BusinessException("参数错误");
        }
        return chatSettingsService.saveKeywordShield(login.getMerId(), chatWwDeviceOpt);
    }

    @PostMapping("/keyword/shield/get")
    @ApiOperation(value = "获取关键词消息屏蔽", notes = "获取关键词消息屏蔽", response = ChatSettingsVO.class)
    public Object getKeywordShield() {
        LoginVO login = LoginContext.getLogin();
//
//        if (!Objects.equals(login.getId(), login.getMerId())) {
//            throw new BusinessException("非主账号, 不允许操作");
//        }

        return chatSettingsService.getKeywordShield(login.getMerId());
    }
}
